// 获取 当天 零时 零分 零秒 的时间 戳
export function h0(timestamp = Date.now()) {
    const target = new Date(timestamp)

    target.setHours(0)
    target.setMinutes(0)
    target.setSeconds(0)
    target.setMilliseconds(0)

    return target.getTime()
}

export function GetQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
    var r = window.location.search.substr(1).match(reg)
    if (r != null) return decodeURI(r[2])
    return null
}
export const formatCount = (num) => {
    if (!num) {
        return 0
    }
    num = num.toString()
    if (num.length <= 4) {
        return num
    }
    let count = num.substr(0, num.length - 4)
    let thousands = Math.round(num.substr(-4, -2))
    return `${count}.${thousands}万`
}
export const getPicUrl = (url, format) => {
    const picURL = 'http://localhost:3011/transfer/image'
    let suffix = '.jpg'
    // picURL + "?pic=" + url + "@480w_300h.webp"
    return `${picURL}?pic=${url}${format + suffix}`
}
export const formatDuration = (second, format) => {
    let date = new Date()
    date = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
    const tempstamp = date.getTime() + second * 1000
    const newDate = new Date(tempstamp)
    const hours = newDate.getHours()
    const minutes = newDate.getMinutes()
    const seconds = newDate.getSeconds()
    date = null
    const toStr = (num) => {
        return num < 10 ? '0' + num : num
    }
    let duration = `${toStr(hours)}:${toStr(minutes)}:${toStr(seconds)}`
    if (format) {
        const formats = format.split(':')
        if (formats.length === 3) {
            duration = duration.match(/\d{2}:\d{2}:\d{2}$/)[0]
        } else if (formats.length === 2) {
            duration = duration.match(/\d{2}:\d{2}$/)[0]
        }
        if (format.indexOf('0') === -1) {
            duration = duration.replace(duration.charAt(0), '')
        }
    }
    return duration
}
export const formatDate = (date, format) => {
    const reg = {
        'M+': date.getMonth() + 1, // 月份
        'd+': date.getDate(), // 日
        'h+': date.getHours(), // 小时
        'm+': date.getMinutes(), // 分
        's+': date.getSeconds(), // 秒
        'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
        S: date.getMilliseconds() // 毫秒
    }
    if (/(y+)/i.test(format)) {
        format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    }
    for (const k in reg) {
        if (new RegExp('(' + k + ')').test(format)) {
            format = format.replace(
                RegExp.$1,
                RegExp.$1.length === 1 ? reg[k] : ('00' + reg[k]).substr(('' + reg[k]).length)
            )
        }
    }
    return format
}
export const getTransitionEndName = (dom) => {
    const cssTransition = ['transition', 'webkitTransition']
    const transitionEnd = {
        transition: 'transitionend',
        webkitTransition: 'webkitTransitionEnd'
    }
    for (const key of cssTransition) {
        if (dom.style[key] !== undefined) {
            return transitionEnd[key]
        }
    }
    return undefined
}
